package com.ugrow.ugrowcommunity.activity.moments.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.moments.commentBean.Data;
import com.ugrow.ugrowcommunity.databinding.CommendItemLayoutBinding;

import java.util.ArrayList;
import java.util.List;

public class CommendAdapter extends RecyclerView.Adapter<CommendAdapter.ViewHolder> {
    private List<Data> list;
    private Context context;
    private ChildCommendAdapter childCommendAdapter;

    public CommendAdapter(List<Data> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.commend_item_layout, parent, false);
        CommendItemLayoutBinding bind = DataBindingUtil.bind(view);
        return new ViewHolder(bind);
    }

    private OnCommentClickListener onCommentClickListener;

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Data data = list.get(position);
        holder.mBinding.mTvNickName.setText(data.getAuthorId());
        holder.mBinding.mTvContent.setText(data.getContent());
        if (data.getChildren() != null) {
            LinearLayoutManager layout = new LinearLayoutManager(context);
            layout.setOrientation(RecyclerView.VERTICAL);
            holder.mBinding.childRecyclerView.setLayoutManager(layout);
            ChildCommendAdapter adapter = new ChildCommendAdapter(data.getChildren());
            holder.mBinding.childRecyclerView.setAdapter(adapter);
            adapter.dispatchUpdate(data.getChildren());
        }


        holder.mBinding.levelOneComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.mBinding.childRecyclerView.getAdapter() !=null){
                    childCommendAdapter = (ChildCommendAdapter) holder.mBinding.childRecyclerView.getAdapter();
                }else {
                    LinearLayoutManager layout = new LinearLayoutManager(context);
                    layout.setOrientation(RecyclerView.VERTICAL);
                    holder.mBinding.childRecyclerView.setLayoutManager(layout);
                    childCommendAdapter = new ChildCommendAdapter(new ArrayList<>());
                    holder.mBinding.childRecyclerView.setAdapter(childCommendAdapter);
                }

                onCommentClickListener.onclickListener(String.valueOf(data.getAuthorId()), "2");
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void dispatchUpdate(List<Data> newList) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack<>(list, newList), true);
        diffResult.dispatchUpdatesTo(this);
        list = newList;
    }

    public void setOnCommentClickListener(OnCommentClickListener onCommentClickListener) {
        this.onCommentClickListener = onCommentClickListener;
    }

    public void addHeaderView(String content, String id) {
        Data data = new Data();
        data.setContent(content);
        data.setAuthorId(id);
        data.setLevel(1);
        ArrayList<Data> list1 = new ArrayList<>(list);
        Log.d("TAG", "addHeaderView: " + content + "  " + id);
        list1.add(0, data);
        dispatchUpdate(list1);
    }

    public void addChildHeaderView(String content, String id) {
        childCommendAdapter.addChildHeaderView(content, id);
    }

    public interface OnCommentClickListener {
        void onclickListener(String parentId, String level);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        CommendItemLayoutBinding mBinding;

        public ViewHolder(@NonNull CommendItemLayoutBinding bind) {
            super(bind.getRoot());
            mBinding = bind;
        }
    }
}
